import { FC, useEffect, useState } from 'react';
import { Space, Card, Button, Form, Input, Checkbox } from 'antd';
import { LockOutlined, UserOutlined } from '@ant-design/icons';

const Index: FC = () => {
  // form表示表单实例
  const [form] = Form.useForm();
  // const [, forceUpdate] = useState({});
  // const [values, setValues] = useState<any>({});

  // To disable submit button at the beginning.
  // useEffect(() => {
  //   forceUpdate({});
  // }, []);

  // const onFinish = (values: any) => {
  //   console.log('Finish:', values);
  // };

  const fn = () => {
    // getFieldsValue用于获取整个表单数据的
    // console.log(form.getFieldsValue());
    // getFieldValue用来获取单个表单数据
    // console.log(form.getFieldValue('username'));
    // getFieldsError用来判断是否所有的验证都通过了
    // console.log(form.getFieldsError());
    // getFieldsError用来判断对应的字段的验证是否通过了
    // console.log(form.getFieldError('username'));
    // isFieldsTouched用来判断字段是否被操作过
    // console.log(form.isFieldsTouched(true));
    // resetFields用于清空表单
    // form.resetFields();

    // setFieldsValue是用来修改表单里面的内容的，initialValues是没有响应式
    // form.setFieldsValue({
    //   username: 'zhangsan',
    //   password: '123',
    // });

    form
      .validateFields()
      .then((value) => {
        console.log(value);
      })
      .catch((err) => {
        console.log(err);
      });
  };

  return (
    <Space direction="vertical" style={{ display: 'flex' }}>
      <Card title="登录表单">
        <Form
          form={form}
          name="horizontal_login"
          // 水平布局
          layout="inline"
          // 默认值
          // initialValues={values}
          // 表单里面的submit按钮点击时触发
          // onFinish={onFinish}
        >
          {/* 每一个表单控件都需要加上Form.Item */}
          <Form.Item
            // name是最后得到的对象的key
            name="username"
            rules={[
              { required: true, message: 'Please input your username!' },
              // {
              //   max: 10,
              //   message: '超出了最大长度',
              // },
              // {
              //   pattern: /^[1][3-9][\d]{9}$/,
              //   message: '请输入正确的手机号',
              // },
            ]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: 'Please input your password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          {/* <Form.Item>
            <Button type="primary" htmlType="submit">
              Log in
            </Button>
          </Form.Item> */}
        </Form>
        <Button type="primary" onClick={fn}>
          Log in
        </Button>
      </Card>

      <Card title="登录表单2">
        <Form
          name="normal_login"
          className="login-form"
          initialValues={{ remember: true }}
          style={{ width: 280 }}
        >
          <Form.Item
            name="username"
            rules={[{ required: true, message: 'Please input your Username!' }]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: 'Please input your Password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          <Form.Item>
            <Form.Item name="remember" valuePropName="checked" noStyle>
              <Checkbox>Remember me</Checkbox>
            </Form.Item>

            <a className="login-form-forgot" href="">
              Forgot password
            </a>
          </Form.Item>

          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              className="login-form-button"
            >
              Log in
            </Button>
            Or <a href="">register now!</a>
          </Form.Item>
        </Form>
      </Card>
    </Space>
  );
};

export default Index;
